package com.lyf.scm.common.util;

import com.lyf.scm.common.enums.BaseSearchColumnEnum;
import com.lyf.scm.common.enums.ExcelExportTitleEnum;
import com.lyf.scm.common.enums.LayeredEnum;
import com.lyf.scm.common.model.BaseNormalDTO;
import com.lyf.scm.common.model.BaseSearchDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 高级搜索工具类
 *
 * @author wangchlei
 * @date 2020/08/12
 */
public class AdvancedSearchUtil {
    /**
     * 获取所有查询条件标识（通用）
     *
     * @param baseSearchDTO
     * @return {@link List <String>}
     **/
    public static List<String> getConditions(BaseSearchDTO baseSearchDTO) {
        List<String> conditions = new ArrayList<>();
        // if (!CollectionUtils.isEmpty(baseSearchDTO.getFatherChannelCodeList())) {
        //     conditions.add(BaseSearchColumnEnum.CHANNEL_CODE.getColumn());
        // }
        // if (!CollectionUtils.isEmpty(baseSearchDTO.getSubChannelCodeList())) {
        //     conditions.add(BaseSearchColumnEnum.SUB_CHANNEL_CODE.getColumn());
        // }
        if (!CollectionUtils.isEmpty(baseSearchDTO.getProvinceCodeList())) {
            conditions.add(BaseSearchColumnEnum.PROVINCE_CODE.getColumn());
        }
        if (!CollectionUtils.isEmpty(baseSearchDTO.getCityCodeList())) {
            conditions.add(BaseSearchColumnEnum.CITY_CODE.getColumn());
        }
        if (!CollectionUtils.isEmpty(baseSearchDTO.getCountyCodeList())) {
            conditions.add(BaseSearchColumnEnum.COUNTY_CODE.getColumn());
        }
        if (!CollectionUtils.isEmpty(baseSearchDTO.getStoreCodeList())) {
            conditions.add(BaseSearchColumnEnum.STORE_CODE.getColumn());
        }
        if (!CollectionUtils.isEmpty(baseSearchDTO.getPriCategoryCodeList())) {
            conditions.add(BaseSearchColumnEnum.PRI_CATEGORY_CODE.getColumn());
        }
        if (!CollectionUtils.isEmpty(baseSearchDTO.getSecCategoryCodeList())) {
            conditions.add(BaseSearchColumnEnum.SEC_CATEGORY_CODE.getColumn());
        }
        if (!CollectionUtils.isEmpty(baseSearchDTO.getTerCategoryCodeList())) {
            conditions.add(BaseSearchColumnEnum.TER_CATEGORY_CODE.getColumn());
        }
        if (!CollectionUtils.isEmpty(baseSearchDTO.getSkuCodeList())) {
            conditions.add(BaseSearchColumnEnum.SKU_CODE.getColumn());
        }
        return conditions;
    }

    /**
     * 统计不同维度时间周期的长度
     *
     * @param weekDateList
     * @return {@link int}
     **/
    public static int getElementsSize(List<Map<String, Date>> weekDateList) {
        return weekDateList.size();
    }

    /**
     * 获取分组key
     *
     * @param baseNormalDTO
     * @param conditions
     * @return {@link String}
     **/
    public static String fetchGroupKey(BaseNormalDTO baseNormalDTO, List<String> conditions) {
        StringBuilder groupKey = new StringBuilder();
        if (conditions.contains(BaseSearchColumnEnum.CHANNEL_CODE.getColumn())) {
            groupKey.append(baseNormalDTO.getChannelCode());
        }
        if (conditions.contains(BaseSearchColumnEnum.SUB_CHANNEL_CODE.getColumn())) {
            groupKey.append(baseNormalDTO.getSubChannelCode());
        }
        if (conditions.contains(BaseSearchColumnEnum.PROVINCE_CODE.getColumn())) {
            groupKey.append(baseNormalDTO.getProvinceCode());
        }
        if (conditions.contains(BaseSearchColumnEnum.CITY_CODE.getColumn())) {
            groupKey.append(baseNormalDTO.getCityCode());
        }
        if (conditions.contains(BaseSearchColumnEnum.COUNTY_CODE.getColumn())) {
            groupKey.append(baseNormalDTO.getCountyCode());
        }
        if (conditions.contains(BaseSearchColumnEnum.STORE_CODE.getColumn())) {
            groupKey.append(baseNormalDTO.getStoreCode());
        }
        if (conditions.contains(BaseSearchColumnEnum.PRI_CATEGORY_CODE.getColumn())) {
            groupKey.append(baseNormalDTO.getPriCategoryCode());
        }
        if (conditions.contains(BaseSearchColumnEnum.SEC_CATEGORY_CODE.getColumn())) {
            groupKey.append(baseNormalDTO.getSecCategoryCode());
        }
        if (conditions.contains(BaseSearchColumnEnum.TER_CATEGORY_CODE.getColumn())) {
            groupKey.append(baseNormalDTO.getTerCategoryCode());
        }
        if (conditions.contains(BaseSearchColumnEnum.SKU_CODE.getColumn())) {
            groupKey.append(baseNormalDTO.getSkuCode());
        }
        return groupKey.toString();
    }

    /**
     * 聚合
     *
     * @param sumValue1Lists
     * @param elementSize
     * @param totalSize
     * @return {@link List<BigDecimal>}
     **/
    public static List<BigDecimal> getMergeResult(List<BigDecimal> sumValue1Lists, int elementSize, int totalSize) {
        List<BigDecimal> mergeResult = new ArrayList<>();
        int x = 0;
        int y = 0;
        while (x < elementSize) {
            BigDecimal item = new BigDecimal("0");
            while (y < totalSize) {
                item = item.add(sumValue1Lists.get(y));
                y += elementSize;
            }
//            if (flag) {
//                item = item.setScale(0, BigDecimal.ROUND_UP);
//            }
            mergeResult.add(item);
            x++;
            y = x;
        }
        return mergeResult;
    }

    /**
     * 预测准确率 =【（销售预测量-真实销量）/真实销量】*100%
     *
     * @param predictedQty
     * @param actualSalesQty
     * @return {@link List<BigDecimal>}
     **/
    public static List<BigDecimal> getForecastAccuracys(List<BigDecimal> predictedQty, List<BigDecimal> actualSalesQty, int elementSize) {
        List<BigDecimal> forecastAccuracys = new ArrayList<>();
        BigDecimal bigDecimal = new BigDecimal("0");
        int index = 0;
        while (index < elementSize) {
            if (actualSalesQty.get(index).compareTo(bigDecimal) == 0) {
                forecastAccuracys.add(bigDecimal);
            } else {
                forecastAccuracys.add(predictedQty.get(index).subtract(actualSalesQty.get(index)).divide(actualSalesQty.get(index), 5, BigDecimal.ROUND_HALF_UP));
            }
            index++;
        }
        return forecastAccuracys;
    }

    /**
     * 目标达成率 =实际销售额/ 目标销售额
     *
     * @param salesTargetAmounts
     * @param salesActualAmount
     * @param elementSize
     * @return {@link List<BigDecimal>}
     **/
    public static List<BigDecimal> getTargetAchievementRateAmount(List<BigDecimal> salesTargetAmounts, List<BigDecimal> salesActualAmount, int elementSize) {
        List<BigDecimal> targetAchievementRateAmount = new ArrayList<>();
        BigDecimal bigDecimal = new BigDecimal("0");
        int index = 0;
        while (index < elementSize) {
            if ((salesTargetAmounts.get(index).compareTo(bigDecimal) == 0)) {
                targetAchievementRateAmount.add(bigDecimal);
            } else {
                targetAchievementRateAmount.add(salesActualAmount.get(index).divide(salesTargetAmounts.get(index), 5, BigDecimal.ROUND_HALF_UP));
            }
            index++;
        }
        return targetAchievementRateAmount;
    }


    /**
     * @param baseNormalDTO
     * @param conditions
     * @return void
     **/
    public static void setGroupAdvancedSearchCondition(BaseNormalDTO baseNormalDTO, List<String> conditions) {
        if (conditions.contains(BaseSearchColumnEnum.PROVINCE_CODE.getColumn())) {
            baseNormalDTO.setAreaDesc(baseNormalDTO.getProvinceName());
        }
        if (conditions.contains(BaseSearchColumnEnum.CITY_CODE.getColumn())) {
            baseNormalDTO.setAreaDesc(baseNormalDTO.getProvinceName() + "/" + baseNormalDTO.getCityName());
        }
        if (conditions.contains(BaseSearchColumnEnum.COUNTY_CODE.getColumn())) {
            baseNormalDTO.setAreaDesc(baseNormalDTO.getProvinceName() + "/" + baseNormalDTO.getCityName() + "/" + baseNormalDTO.getCountyName());
        }
        if (conditions.contains(BaseSearchColumnEnum.STORE_CODE.getColumn())) {
            baseNormalDTO.setAreaDesc(baseNormalDTO.getProvinceName() + "/" + baseNormalDTO.getCityName() + "/" + baseNormalDTO.getCountyName() + "/" + baseNormalDTO.getStoreName());
        }
        if (conditions.contains(BaseSearchColumnEnum.PRI_CATEGORY_CODE.getColumn())) {
            //去除展示skuCode
            baseNormalDTO.setSkuCode(null);
            baseNormalDTO.setCategoryDesc(baseNormalDTO.getPriCategoryName());
        }
        if (conditions.contains(BaseSearchColumnEnum.SEC_CATEGORY_CODE.getColumn())) {
            baseNormalDTO.setSkuCode(null);
            baseNormalDTO.setCategoryDesc(baseNormalDTO.getPriCategoryName() + "/" + baseNormalDTO.getSecCategoryName());
        }
        if (conditions.contains(BaseSearchColumnEnum.TER_CATEGORY_CODE.getColumn())) {
            baseNormalDTO.setSkuCode(null);
            baseNormalDTO.setCategoryDesc(baseNormalDTO.getPriCategoryName() + "/" + baseNormalDTO.getSecCategoryName() + "/" + baseNormalDTO.getTerCategoryName());
        }
        if (conditions.contains(BaseSearchColumnEnum.SKU_CODE.getColumn())) {
            baseNormalDTO.setCategoryDesc(baseNormalDTO.getPriCategoryName() + "/" + baseNormalDTO.getSecCategoryName() + "/" + baseNormalDTO.getTerCategoryName());
            baseNormalDTO.setSkuDesc(baseNormalDTO.getSkuName()+"/"+baseNormalDTO.getSkuCode());
        } else {
            baseNormalDTO.setSkuCode(null);
        }
    }

    /**
     * excel每行数据设置 加入区域，品类，商品  信息
     *
     * @param baseNormalDTO
     * @param itemRow
     * @return void
     **/
    public static void setExcelRowAdvancedSearchCondition(BaseNormalDTO baseNormalDTO, List<Object> itemRow) {
        if (!StringUtils.isEmpty(baseNormalDTO.getAreaDesc())) {
            itemRow.add(baseNormalDTO.getAreaDesc());
        }
        if (!StringUtils.isEmpty(baseNormalDTO.getCategoryDesc())) {
            itemRow.add(baseNormalDTO.getCategoryDesc());
        }
        if (!StringUtils.isEmpty(baseNormalDTO.getSkuDesc())) {
            itemRow.add(baseNormalDTO.getSkuDesc());
        }
    }

    /**
     * 直营   excel每行数据设置 加入渠道，区域，品类，商品  信息
     *
     * @param baseNormalDTO
     * @param itemRow
     * @return void
     **/
    public static void setRedirectExcelRowAdvancedSearchCondition(BaseNormalDTO baseNormalDTO, List<Object> itemRow) {
        if (!StringUtils.isEmpty(baseNormalDTO.getAreaDesc())) {
            itemRow.add(baseNormalDTO.getAreaDesc());
        }
        if (!StringUtils.isEmpty(baseNormalDTO.getCategoryDesc())) {
            itemRow.add(baseNormalDTO.getCategoryDesc());
        }
        if (!StringUtils.isEmpty(baseNormalDTO.getSkuDesc())) {
            itemRow.add(baseNormalDTO.getSkuDesc());
        }
    }

    /**
     * 设置导出excel标题中的搜索条件标题（渠道，区域，品类，商品）
     *
     * @param heads
     * @param conditions
     * @return void
     **/
    public static void getExcelAdvancedSearchTitle(List<List<String>> heads, List<String> conditions) {
        if (conditions.contains(BaseSearchColumnEnum.PROVINCE_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.AREA_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.CITY_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.AREA_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.COUNTY_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.AREA_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.STORE_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.AREA_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.PRI_CATEGORY_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.CATEGORY_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.SEC_CATEGORY_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.CATEGORY_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.TER_CATEGORY_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.CATEGORY_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.SKU_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.CATEGORY_TITLE.getTitle());
            setList(heads, ExcelExportTitleEnum.SKU_TITLE.getTitle());
        }
    }

    /**
     * （直营）设置导出excel标题中的搜索条件标题（区域，品类，商品）
     *
     * @param heads
     * @param conditions
     * @return void
     **/
    public static void getRedirectExcelAdvancedSearchTitle(List<List<String>> heads, List<String> conditions) {
        if (conditions.contains(BaseSearchColumnEnum.PROVINCE_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.AREA_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.CITY_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.AREA_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.COUNTY_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.AREA_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.STORE_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.AREA_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.PRI_CATEGORY_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.CATEGORY_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.SEC_CATEGORY_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.CATEGORY_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.TER_CATEGORY_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.CATEGORY_TITLE.getTitle());
        }
        if (conditions.contains(BaseSearchColumnEnum.SKU_CODE.getColumn())) {
            setList(heads, ExcelExportTitleEnum.CATEGORY_TITLE.getTitle());
            setList(heads, ExcelExportTitleEnum.SKU_TITLE.getTitle());
        }
    }

    public static void setList(List<List<String>> heads, String title) {
        List<String> list = new ArrayList<>();
        list.add(title);
        heads.add(list);
    }

    /**
     * 设置导出excel标题中的日期标题
     *
     * @param heads
     * @param dateTitles
     * @return void
     **/
    public static void getExcelDateTitle(List<List<String>> heads, List<String> dateTitles) {
        dateTitles.forEach(item -> {
            List<String> list = new ArrayList<>();
            list.add(item);
            heads.add(list);
        });
    }

    /**
     * 获取excel标题 同时返回 高级搜索标题的数量
     *
     * @param heads
     * @param conditions
     * @param dateTitles
     * @return void
     **/
    public static int setExcelTitles(List<List<String>> heads, List<String> conditions, List<String> dateTitles) {
        //获取高级搜索标题
        AdvancedSearchUtil.getExcelAdvancedSearchTitle(heads, conditions);
        int result = heads.size();
        //加入指标标题
        List<String> targetTitle = new ArrayList<>();
        targetTitle.add(ExcelExportTitleEnum.TARGET_TITLE.getTitle());
        heads.add(targetTitle);
        //加入日期标题
        getExcelDateTitle(heads, dateTitles);
        return result;
    }

    /**
     * (直营) 获取excel标题 同时返回 高级搜索标题的数量
     *
     * @param heads
     * @param conditions
     * @param dateTitles
     * @return void
     **/
    public static int setRedirectExcelTitles(List<List<String>> heads, List<String> conditions, List<String> dateTitles) {
        //获取高级搜索标题
        getRedirectExcelAdvancedSearchTitle(heads, conditions);
        int result = heads.size();
        //加入指标标题
        List<String> targetTitle = new ArrayList<>();
        targetTitle.add(ExcelExportTitleEnum.TARGET_TITLE.getTitle());
        heads.add(targetTitle);
        //加入日期标题
        getExcelDateTitle(heads, dateTitles);
        return result;
    }

    /**
     * @param baseSearchDTO
     * @return {@link boolean}
     **/
    public static boolean validGmvInfo(BaseSearchDTO baseSearchDTO) {
        //市，县，门店
        if (!CollectionUtils.isEmpty(baseSearchDTO.getCityCodeList())) {
            return false;
        }
        if (!CollectionUtils.isEmpty(baseSearchDTO.getCountyCodeList())) {
            return false;
        }
        if (!CollectionUtils.isEmpty(baseSearchDTO.getStoreCodeList())) {
            return false;
        }
        //中，小品类，sku
        if (!CollectionUtils.isEmpty(baseSearchDTO.getSecCategoryCodeList())) {
            return false;
        }
        if (!CollectionUtils.isEmpty(baseSearchDTO.getTerCategoryCodeList())) {
            return false;
        }
        if (!CollectionUtils.isEmpty(baseSearchDTO.getSkuCodeList())) {
            return false;
        }
        //商品分层
        if (!CollectionUtils.isEmpty(baseSearchDTO.getLayeredCodeList())) {
            return baseSearchDTO.getLayeredCodeList().containsAll(LayeredEnum.getAllType());
        }
        return true;
    }

    public static List<BigDecimal> getBlankCount(int elementsSize) {
        List<BigDecimal> blankCount = new ArrayList<>();
        BigDecimal bigDecimal = new BigDecimal("0");
        for (int i = 0; i < elementsSize; i++) {
            blankCount.add(bigDecimal);
        }
        return blankCount;
    }
}
